//
//  NewStockViewController.m
//  investevent
//
//  Created by jiangshao on 16/3/5.
//  Copyright © 2016年 com.scj.investevent. All rights reserved.
//

#import "NewStockViewController.h"
#import "NewStockCell.h"
#import "LocalNotificationService.h"



@interface NewStockViewController (){
    UIButton  *settingbtn;
}

@end

@implementation NewStockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title=@"新股发行日历";
    _tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    datas=[[NSMutableArray alloc]init];
//    _tableView.contentInset=UIEdgeInsetsMake(64, 0, 49, 0);
    NSInteger offset=64;
    if(kDevice_Is_iPhoneX){
        offset=offset+24;
    }
    
   
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
        make.top.equalTo(self.view).offset(offset);
        make.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-49);
    }];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor=[UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator=NO;
    [_tableView addPullToRefreshWithActionHandler:^{
        if (!_isLoadingData) {[self reloadData];}
    }];
    [_tableView.pullToRefreshView setTitle:@"下拉刷新" forState:SVPullToRefreshStateAll];
    [_tableView.pullToRefreshView setTitle:@"松开刷新" forState:SVPullToRefreshStateTriggered];
    [_tableView.pullToRefreshView setTitle:@"正在加载" forState:SVPullToRefreshStateLoading];
    [_tableView registerNib:[UINib nibWithNibName:@"NewStockCell" bundle:nil] forCellReuseIdentifier:@"NewStockCell"];
    UIView *view= [self addHeadView];
    _tableView.tableHeaderView=view;
    newStockDao=[[NewStockDao alloc]initWithDelagate:self];
     [self reloadData];
    
    self.tabBarController.tabBar.tintColor=themeColor;
    
}

-(UIView *)addHeadView{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenviewwidth, 45)];
    view.backgroundColor=themeColor;
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, screenviewwidth/4, view.frame.size.height)];
    name.text=@"新股名称";
    name.font=Font(15);
    name.textColor=[UIColor whiteColor];
    name.textAlignment=NSTextAlignmentCenter;
    [view addSubview:name];
    
    
    UILabel *fxs=[[UILabel alloc]initWithFrame:CGRectMake(name.frame.size.width, 0, screenviewwidth/4, view.frame.size.height)];
    fxs.text=@"网发/总发";
    fxs.font=Font(15);
    fxs.textColor=[UIColor whiteColor];
    fxs.textAlignment=NSTextAlignmentCenter;
    [view addSubview:fxs];

    
    UILabel *fxj=[[UILabel alloc]initWithFrame:CGRectMake(fxs.frame.size.width+fxs.frame.origin.x, 0, screenviewwidth/4, view.frame.size.height)];
    fxj.text=@"发行价/市盈";
    fxj.font=Font(15);
    fxj.textColor=[UIColor whiteColor];
    fxj.textAlignment=NSTextAlignmentCenter;
    [view addSubview:fxj];

    
    UILabel *sgsx=[[UILabel alloc]initWithFrame:CGRectMake(fxj.frame.size.width+fxj.frame.origin.x, 0, screenviewwidth/4, view.frame.size.height)];
    sgsx.text=@"申购上限";
    sgsx.font=Font(15);
    sgsx.textColor=[UIColor whiteColor];
    sgsx.textAlignment=NSTextAlignmentCenter;
    [view addSubview:sgsx];
    return view;
}

-(void)reloadData{
    [newStockDao getNewStock];
}


-(void)getRemotedata:(NSMutableDictionary*)dic{
    
    if ([dic objectForKey:@"getNewStock"]) {
        [_tableView.pullToRefreshView stopAnimating];
        [datas removeAllObjects];
        [datas addObjectsFromArray:[[[dic objectForKey:@"getNewStock" ] objectForKey:@"data"]objectForKey:@"data"]];
        for (long i=datas.count-1;i>=0;i--) {
            NSDictionary *dic=datas[i];
            NSString *currentDate=[NSDate currentDateStringWithFormat:@"yyyy-MM-dd"];
           NSString *sgrq=[dic objectForKey:@"sgrq"];
          if ([sgrq compare:currentDate]==NSOrderedAscending) {
              [datas removeObjectAtIndex:i];
           }
        }
        NSArray *array=[[datas reverseObjectEnumerator] allObjects];
        [datas removeAllObjects];
        [datas addObjectsFromArray:array];
        
        NSUserDefaults *localUserDefaults=[NSUserDefaults standardUserDefaults];
        if ([localUserDefaults boolForKey:@"noficationSetting"]) {
            [LocalNotificationService cancelAllLocalNotification];
            [LocalNotificationService registerLocalNotification:datas];
        }
        [_tableView reloadData];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return datas.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict=datas[indexPath.row];
    NewStockCell *newStockCell= (NewStockCell *)[tableView dequeueReusableCellWithIdentifier:@"NewStockCell"];
    newStockCell.selectionStyle=UITableViewCellSelectionStyleNone;
    [newStockCell setContent:dict];
    return newStockCell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [[NavService getNav] setNavigationBarHidden:NO];
    self.tabBarController.navigationItem.title=@"新股申购";
    settingbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    NSUserDefaults *localUserDefaults=[NSUserDefaults standardUserDefaults];
    BOOL noficationSetting=[localUserDefaults boolForKey:@"noficationSetting"];
    if (noficationSetting) {
        [settingbtn setTitle:@"关闭提醒" forState:UIControlStateNormal];
    }else{
         [settingbtn setTitle:@"开启提醒" forState:UIControlStateNormal];;
    }
    settingbtn.titleLabel.font=Font(16);
    [settingbtn setTitleColor:themeColor forState:UIControlStateNormal];
    settingbtn.frame = CGRectMake(0,7,80, 30);
    [settingbtn addTarget:self action:@selector(settingAll:) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *settingall = [[UIBarButtonItem alloc] initWithCustomView:settingbtn];
    self.tabBarController.navigationItem.rightBarButtonItem=settingall;
}

-(IBAction)settingAll:(id)sender{
    NSUserDefaults *localUserDefaults=[NSUserDefaults standardUserDefaults];
    BOOL noficationSetting=[localUserDefaults boolForKey:@"noficationSetting"];
    if (noficationSetting) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"你已关闭新股提醒"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        [LocalNotificationService cancelAllLocalNotification];
        [localUserDefaults setBool:NO  forKey:@"noficationSetting"];
        [localUserDefaults synchronize];
        [settingbtn setTitle:@"开启提醒" forState:UIControlStateNormal];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"你已开启新股提醒"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        [localUserDefaults setBool:YES  forKey:@"noficationSetting"];
        [localUserDefaults synchronize];
        [LocalNotificationService cancelAllLocalNotification];
        [LocalNotificationService registerLocalNotification:datas];
        [settingbtn setTitle:@"关闭提醒" forState:UIControlStateNormal];
    }
    
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView*view=[self addHeadView];
//    return view;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 45;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
